//
//  MYGMyAnswerViewController.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/25.
//

import UIKit

class MYGMyAnswerViewController: MYGBaseController {

    private var listModels:  [MYGCommunityListModel] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData(isRefreshing: true)
    }
    
    // MARK: - UI
    private func configUI() {
        view.backgroundColor = .white
        
        title = "我的回答"
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            //make.top.equalTo(searchTextFiled.snp.bottom).offset(10)
            make.left.top.right.bottom.equalToSuperview()
        }
        
    }
    private func reloadListView() {
        let cellModels = listModels.map({ listModel in
            return MYGCommunityCellModel(with: listModel)
        })
        tableView.reload(with: cellModels)
    }
    private lazy var tableView: MYGCommunityListView = {
        let listView = MYGCommunityListView(frame: .zero)
        listView.loadDataAction = {[weak self] isRefreshing in
            self?.loadData(isRefreshing: isRefreshing)
        }
        listView.communityListAction = {[weak self] (type, index) in
            switch type {
            case .selected:
                self?.listItemDidSelect(index: index)
            case .like:
                self?.listItemDidClickLike(index: index)
            default: break
            }
            
            
        }
        return listView
    }()
    
    private func listItemDidSelect(index: Int) {
        let listModel = listModels[index]
        let vc = MYGCommunityQuestionController()
        vc.communityId = listModel.id
        vc.isMyAnswer = true
        navigationController?.pushViewController(vc)
    }
    
    private func listItemDidClickLike(index: Int) {
        let listModel = listModels[index]
        let likeStatus = listModel.communities_patient.is_like == 0 ? "1" : "0"
        MYGLoading.show(self.view)
        
        MMYGNetwork<MYGCommunityApi, MYGEmptyModel>.request(target: .communitiesLike(type: "10", likeStatus: likeStatus, id: String(listModel.communities_patient.id))) { [weak self] item in
               
               guard let self = self else { return }
               MYGLoading.hide(self.view)
   //            guard let response = response else { return }
            if self.listModels[index].communities_patient.is_like == 1 {
                self.listModels[index].communities_patient.likes = self.listModels[index].communities_patient.likes - 1
                self.listModels[index].communities_patient.is_like = 0
               } else {
                   self.listModels[index].communities_patient.likes = self.listModels[index].communities_patient.likes + 1
                   self.listModels[index].communities_patient.is_like = 1
               }
               self.reloadListView()
               
       } failed: { _, error in
           MYGLoading.hide(self.view)
           MMYGHUD.showToast(text: error)
       }
    }
    
    private func loadData(isRefreshing: Bool) {
        MYGLoading.show()
        
        MMYGNetwork<MYGCommunityApi, MYGCommunityModel>.request(target: .my_answersList) {[weak self] response in
            MYGLoading.hide()
            guard let response = response?.data else {
                self?.tableView.endRefreshing(noMoreData: true)
                return
            }
            
            if isRefreshing {
                self?.listModels.removeAll()
            }
            
            self?.listModels = response
            self!.reloadListView()
            self?.tableView.endRefreshing(noMoreData: true)

        } failed: {[weak self] _, error in
            MYGLoading.hide()
            self?.tableView.endRefreshing(noMoreData: true)
            MMYGHUD.showToast(text: error)
        }
        
    }
}
